﻿using AdminSystem.Application.Commands.TraceabilityCommands;
using AdminSystem.Application.Dtos;
using AdminSystem.Domain.Entities;
using AdminSystem.EntityFramworkCore;
using MediatR;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;

namespace AdminSystem.WebAPI.Controllers
{
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class TraceabilityController : ControllerBase
    {
        private readonly IMediator mediator;
        public TraceabilityController(IMediator mediator)
        {
            this.mediator = mediator;
        }
        [HttpGet]
        public async Task<APIResult<List<TraceabilityDTO>>> GetTraceability()
        {
            TraceabilityQueryCommand command = new TraceabilityQueryCommand();
            return await mediator.Send(command);
        }
        [HttpPost]
        public async Task<APIResult<object>> AddTraceability(TraceabilityAddCommand command)
        {
            return await mediator.Send(command);
        }
        /// <summary>
        /// 上传图片
        /// </summary>
        /// <param name="file"></param>
        /// <returns></returns>
        [HttpPost]
        public IActionResult Upload(IFormFile file)
        {
            if (file == null || file.Length == 0)
            {
                return BadRequest("未选择文件或文件为空");
            }
            var fileName = file.FileName;
            var filePath = Directory.GetCurrentDirectory() + @"\wwwroot\pic\" + fileName;

            using (var stream = new FileStream(filePath, FileMode.Create))
            {
                file.CopyTo(stream);
            }
            var Url = "/pic/" + fileName;

            return Ok(Url);
        }
        [HttpPost]
        public async Task<APIResult<object>> UpdateTraceability(TraceabilityUpdCommand command)
        {
            return await mediator.Send(command);
        }
        [HttpPost]
        public async Task<APIResult<object>> DelTraceability(TraceabilityDelCommand command)
        {
            return await mediator.Send(command);
        }
        [HttpGet]
        public async Task<APIResult<TraceabilityDTO>> GetTracebilityDetile(string TraceabilityId)
        {
            TracebilitysDetileCommands command = new TracebilitysDetileCommands();
            command.TraceabilityId = TraceabilityId;
            return await mediator.Send(command);
        }

    }
}
